from pathlib import Path
from pydantic import BaseModel
from pydantic_settings import BaseSettings, SettingsConfigDict

BASE_DIR = Path(__file__).parent.parent
DB_PATH = BASE_DIR / "price-analysis.db"
AUX_DB_PATH = BASE_DIR / "guides.db"

ORIGINS = [
    "http://localhost",
]

ALL_METHODS = [
    "DELETE",
    "GET",
    "HEAD",
    "POST",
    "OPTIONS",
]

SAFELISTED_HEADERS = [
    "Accept",
    "Accept-Language",
    "Accept-Encoding",
    "Connection",
    "Content-Language",
    "Content-Type",
    "Set-Cookie",
    "Access-Control-Allow-Headers",
    "Access-Control-Allow-Origin",
    "Authorization",
]


class DbSettings(BaseModel):
    url: str = f"sqlite+aiosqlite:///{DB_PATH}"
    echo: bool = False
    # echo: bool = True


class AuxiliaryDbSettings(BaseModel):
    url: str = f"sqlite+aiosqlite:///{AUX_DB_PATH}"
    echo: bool = False


class Settings(BaseSettings):
    yandex_api_key: str
    yandex_api_key_identifier: str
    yandex_id_catalog: str
    yandex_url: str
    api_key: str
    supported_formats: str
    supported_spreadsheet: str
    google_pass: str

    polars_skip_cpu_check: bool = True
    incoming_path: str = f"{BASE_DIR}/data/incoming"
    ocr_data_path: str = f"{BASE_DIR}/data/ocr"
    page_image_path: str = f"{BASE_DIR}/data/page_image"
    main_report_path: str = f"{BASE_DIR}/data/main_report"
    offer_request_path: str = f"{BASE_DIR}/data/offer_request"

    gm_sender: str = "sentry653@gmail.com"
    gm_smtp: str = "smtp.gmail.com"
    gm_port: int = 587

    incoming_filename: str = "{id}_{file_name}"
    api_v1_prefix: str = "/api/v1"
    db: DbSettings = DbSettings()
    aux_db: AuxiliaryDbSettings = AuxiliaryDbSettings()
    origins: list[str] = ORIGINS
    all_methods: list[str] = ALL_METHODS

    safe_listed_headers: list[str] = SAFELISTED_HEADERS

    model_config = SettingsConfigDict(
        env_file=".env",
        env_file_encoding="utf-8",
    )

    def get_support_formats_list(self):
        return self.supported_formats.split(",")

    def get_support_spreadsheet_list(self):
        return self.supported_spreadsheet.split(",")


settings = Settings()
